<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GO EXEC</title>
    <style>
        .warn {
            font-family: 'Consolas', 'Courier New', monospace; /* 设置等宽字体以获得更好的代码阅读体验 */
            font-size: 14px; /* 设置字体大小 */
            line-height: 1.6; /* 调整行高以优化阅读 */
            color: rgba(61, 143, 43, 1); /* 设置文本颜色 */
            background-color: #f5f5f5; /* 设置背景色 */
            padding: 10px; /* 添加内边距 */
            overflow-x: auto; /* 自动添加水平滚动条，如果内容超出容器宽度 */
            white-space: pre-wrap; /* 保持换行符并允许自动换行 */
            margin-top: 7px;
            margin-bottom: 0;
        }

        .error {
            font-family: 'Consolas', 'Courier New', monospace; /* 设置等宽字体以获得更好的代码阅读体验 */
            font-size: 14px; /* 设置字体大小 */
            line-height: 1.6; /* 调整行高以优化阅读 */
            color: rgba(161, 28, 28, 1); /* 设置文本颜色 */
            background-color: #f5f5f5; /* 设置背景色 */
            padding: 10px; /* 添加内边距 */
            overflow-x: auto; /* 自动添加水平滚动条，如果内容超出容器宽度 */
            white-space: pre-wrap; /* 保持换行符并允许自动换行 */
            margin-top: 7px;
            margin-bottom: 0;
        }

        .console {
            font-family: 'Consolas', 'Courier New', monospace; /* 设置等宽字体以获得更好的代码阅读体验 */
            font-size: 14px; /* 设置字体大小 */
            line-height: 1.6; /* 调整行高以优化阅读 */
            color: #333; /* 设置文本颜色 */
            background-color: #f5f5f5; /* 设置背景色 */
            padding: 10px; /* 添加内边距 */
            overflow-x: auto; /* 自动添加水平滚动条，如果内容超出容器宽度 */
            white-space: pre-wrap; /* 保持换行符并允许自动换行 */
            margin-top: 7px;
            margin-bottom: 0;
        }
    </style>
</head>
<body>

<pre id="headDiv" class="warn">wait for exec ...</pre>
<pre id="consoleDiv" class="console" hidden></pre>
<pre id="warnDiv" class="warn" hidden></pre>
<pre id="errorDiv" class="error" hidden></pre>

<script>
    let consoleDiv = document.getElementById('consoleDiv');
    let headDiv = document.getElementById('headDiv');
    let warnDiv = document.getElementById('warnDiv');
    let errorDiv = document.getElementById('errorDiv');

    async function callExec(cmdUrl) {
        const response = await fetch('/exec' + cmdUrl);
        if (!response.ok) {
            errorDiv.hidden = false
            errorDiv.innerText = `HTTP error! status: ${response.status}` + '\n' + await response.text();
            errorDiv.textContent += await response.text()
            return;
        }
        return await response.text();
    }

    async function callLog(execId) {
        const response = await fetch('/console/' + execId)
        const data = await response.text();
        headDiv.hidden = true

        if (response.status === 200) {
            consoleDiv.hidden = false
            consoleDiv.textContent += data + '\n'
            return true;
        }

        if (response.status === 204) {
            warnDiv.hidden = false
            warnDiv.innerText = "Request cmd done.";
            return false
        }

        errorDiv.hidden = false
        errorDiv.innerText = `HTTP error! status: ${response.status}` + '\n' + data;
        return false
    }

    (async () => {

        let pathname = new URL(window.location.href).pathname;
        if (pathname.endsWith('/')) {
            pathname = pathname.slice(0, -1);
        }

        if (pathname.length === 0) {
            errorDiv.hidden = false
            errorDiv.innerText = "cmd is empty."
            return;
        }
        const execId = await callExec(pathname);

        for (let continueQuery = true; continueQuery;) {
            try {
                continueQuery = await callLog(execId);
            } catch (error) {
                console.error('Error:', error);
                break;
            }
        }
    })();
</script>

</body>
</html>
